<template>
  <div class="app-container">
    <div style="display: flex;">
      <img
        style="width: 200px;height: 150px;flex-shrink: inherit;"
        src="https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80"
        alt=""
      >
      <div style="display: flex;flex:1;flex-direction: column;margin-left: 5px;">
        <div style="display: flex;align-items: center;justify-content: space-between;">
          <h4 style="margin-top: 5px;margin-bottom: 0px;">这是标题</h4><small>已完结</small>
        </div>
        <p style="margin:5px 0">
          <small>"般候亲品其不提那才产应起近压存。装海信目商么信造老受提龙北形经。受展取行化了学知不制直满开林现子就基。山类原边持于今便单音积转要。厂省或流要增需原传群越应点号影易往较。"</small>
        </p>
        <p style="margin:5px 0">
          <small style="color: red;">$700</small>
        </p>
        <el-button-group>
          <el-button type="warning">上架</el-button>
          <el-button type="default">设为更新中</el-button>
        </el-button-group>
      </div>

    </div>
    <div
      class="filter-container"
      style="display: flex;align-items: center;justify-content: space-between;margin-top: 20px;"
    >
      <el-button size="mini" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="openCourse">
        新增
      </el-button>
      <div>
        <el-select
          v-model="listQuery.status"
          size="mini"
          style="width: 140px;margin-right: 10px;"
          class="filter-item"
          @change="handleFilter"
        >
          <el-option v-for="(item,k) in statusOptions" :key="k" :label="item" :value="k">
            <span style="float: left">{{ item }}</span>
          </el-option>
        </el-select>
        <el-input
          v-model="listQuery.title"
          size="mini"
          placeholder="标题"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
        <el-button v-waves size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          搜索
        </el-button>
      </div>
    </div>
    <el-table
      ref="dragTable"
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="ID" width="65">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="100px" label="课程标题">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程描述" min-width="150px">
        <template slot-scope="{row}">
          <div style="display: flex;">
            <img style="width: 100px;height:50px;" :src="row.cover" alt="">
            <div style="display: flex;flex-direction: column;margin-left: 10px;">
              <span>{{ row.title }}</span>
              <span style="color: red;">￥{{ row.price }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status===0?'danger':'success'">
            {{ row.status | statusFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="Actions" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-popconfirm style="margin-left: 10px;" title="是否要删除该记录" @onConfirm="handleDelete(row,$index)">
            <el-button v-if="row.status!='deleted'" slot="reference" size="mini" type="danger">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Drag" width="80">
        <template slot-scope="{}">
          <svg-icon class="drag-handler" icon-class="drag" />
        </template>
      </el-table-column>
    </el-table>
    <choose-course ref="chooseCourse" />
  </div>
</template>
<script>
  import {
    fetchDetail,
    fetchDetailList
  } from '@/api/column'
  import Sortable from 'sortablejs'
  import chooseCourse from '@/components/chooseCourse/index.vue'
  import waves from '@/directive/waves' // waves directive
  let id = null
  const statusOptions = {
    0: '已下架',
    1: '已上架'
  }
  export default {
    components: {
      chooseCourse
    },
    beforeRouteEnter(to, from, next) {
      id = to.query.id
      next()
    },
    directives: {
      waves
    },
    filters: {
    statusFilter(status) {
      return statusOptions[status]
    }

  },
    data() {
      return {
        list: null,
        total: null,
        listLoading: true,
        sortable: null,
        oldList: [],
        newList: [],
        statusOptions,
        listQuery: {
          status: undefined,
          title: undefined
        },
        detail: {
          id: undefined,
          title: undefined,
          cover: undefined,
          try: undefined,
          conent: undefined,
          sub_count: undefined,
          price: undefined,
          status: undefined,
          isend: undefined
        }
      }
    },
    created() {
      this.getList()
      this.getData()
    },
    methods: {
      openCourse() {
        this.$refs.chooseCourse.show((chooseList) => {
          this.list = [...this.list, ...chooseList]
        }, 5)
      },
      handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
      handleFilter() {},
      getData() {
        fetchDetail({
          id
        }).then(res => {
          console.log(res)
          this.detail = res
        })
      },
      handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
      async getList() {
        this.listLoading = true
        const {
          data
        } = await fetchDetailList(this.listQuery)
        console.log(data)
        this.list = data.items
        this.total = data.total
        this.listLoading = false
      },
      setSort() {
        const el = this.$refs.dragTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
        this.sortable = Sortable.create(el, {
          ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
          setData: function(dataTransfer) {
            // to avoid Firefox bug
            // Detail see : https://github.com/RubaXa/Sortable/issues/1012
            dataTransfer.setData('Text', '')
          },
          onEnd: evt => {
            const targetRow = this.list.splice(evt.oldIndex, 1)[0]
            this.list.splice(evt.newIndex, 0, targetRow)

            // for show the changes, you can delete in you code
            const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
            this.newList.splice(evt.newIndex, 0, tempIndex)
          }
        })
      },
      getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
    }
  }

</script>

<style>
  .sortable-ghost {
    opacity: .8;
    color: #fff !important;
    background: #42b983 !important;
  }

</style>

<style scoped>
  .icon-star {
    margin-right: 2px;
  }

  .drag-handler {
    width: 20px;
    height: 20px;
    cursor: pointer;
  }

  .show-d {
    margin-top: 15px;
  }

</style>
